package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.Coursemark;
import com.czh.springboot04.service.CoursemarkService;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/coursemark")
@CrossOrigin
public class CoursemarkController {
    @Autowired
    private CoursemarkService coursemarkService;

    @PostMapping("/addCoursemark")
    public R addCoursemark(@RequestBody Coursemark coursemark) {
        coursemark.setCreateTime(new Date());
        int i = coursemarkService.addCoursemark(coursemark);
        if (i > 0) {
            return R.ok("添加成功");
        }else {
            return R.error("添加失败");
        }
    }
    @DeleteMapping("/deleteCoursemark")
    public R deleteCoursemark(int id) {
        int i = coursemarkService.deleteCoursemark(id);
        if (i > 0) {
            return R.ok("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    @PutMapping("/updateCoursemark")
    public R updateCoursemark(@RequestBody Coursemark coursemark) {
        int i = coursemarkService.updateCoursemark(coursemark);
        if (i != 0) {
            return R.ok("修改成功");
        }else {
            return R.error("修改失败");
        }
    }

    @GetMapping("/selectCoursemark")
    public R selectCoursemark(int pageNum, int pageSize,String nickName,String courseName) {
        PageInfo<Coursemark> coursemark = coursemarkService.selectCoursemark(pageNum, pageSize, nickName, courseName);
        if (coursemark != null) {
            return R.ok(coursemark);
        }else {
            return R.error("查询失败");
        }
    }

    //根据课程id查询课程
    @GetMapping("/findCoursemarkByCourseId")
    public R findCoursemarkByCourseId(int id) {
        List<Coursemark> coursemark = coursemarkService.findCoursemarkByCourseId(id);
        if (coursemark!= null) {
            return R.ok(coursemark);
        }else {
            return R.error("查询失败");
        }
    }



}
